package org.example;
import com.qiniu.util.Auth;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

public class UploadImgs {

    static String filePath = "/Users/yesong/IdeaProjects/blogPics/toolsScripte/qiniu/qiniuKey.txt";
    static String accessKey = "access key";
    static String secretKey = "secret key";
    static String bucket = "sid2656";
    static String localDir = "/Users/yesong/IdeaProjects/blogPics/";
    static String uploadDirs = "318ride,2017before,experience,landscape,lifes,parentBJ";

    static Configuration cfg = null;
    static UploadManager uploadManager = null;

    public static void init() {
        // 设置账号的AK、SK
        //构造一个带指定 Region 对象的配置类
        cfg = Configuration.create(Region.region0());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
        //...其他参数参考类注释

        uploadManager = new UploadManager(cfg);

        try (Scanner scanner = new Scanner(new File(filePath))) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
                if (line.startsWith("ak:")) {
                    accessKey = line.substring(3);
                } else if (line.startsWith("sk:")) {
                    secretKey = line.substring(3);
                }
            }
            System.out.println("ak str:"+accessKey);
            System.out.println("sk str:"+secretKey);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        init();
        processFolder(new File(localDir),true);
    }


    public static void processFolder(File folder,boolean isParent) {
        if (folder.getName().equals(".git") || folder.getName().equals(".idea")) {
            return;
        }

        File[] files = folder.listFiles();
        if (files != null) { // 如果文件夹不为空
            for (File file : files) {
                if (file.isDirectory()) { // 如果是文件夹
                    if (!needProcess(isParent,file.getName()) ||file.getName().equals(".git") || file.getName().equals(".idea")) {
                        continue;
                    }
                    System.out.println("Processing folder: " + file.getPath());
                    processFolder(file,false); // 递归调用
                } else if (file.isFile()){ // 如果是文件
                    processFile(file); // 处理文件
                }
            }
        }
    }

    private static boolean needProcess(boolean isParent, String name) {
        if (isParent) {
            return uploadDirs.contains(name);
        }
        return true;
    }

    public static void processFile(File file) {
        String fileName = file.getName();
        if (fileName.endsWith(".jpg") || fileName.endsWith(".png")|| fileName.endsWith(".PNG")|| fileName.endsWith(".JPG")) { // 如果是图片文件
            // 在这里添加处理图片文件的代码
            String localFilePath = file.getAbsolutePath();
            String key = file.getPath().replace(localDir,"");

            System.out.println("upload file path: " + localFilePath);
            System.out.println("upload file: " + key);

//            doUpload(localFilePath, key);
        }
    }

    public static void doUpload(String localFilePath, String key) {
        //如果是Windows情况下，格式是 D:\\qiniu\\test.png
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        try {
            Auth auth = Auth.create(accessKey, secretKey);
            String upToken = auth.uploadToken(bucket);

            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            ex.printStackTrace();
            if (ex.response != null) {
                System.err.println(ex.response);

                try {
                    String body = ex.response.toString();
                    System.err.println(body);
                } catch (Exception ignored) {
                }
            }
        } finally {

        }

    }
}